from PyFlac.Carbon import CarbonUtils
from PyFlac.Carbon import CarbonJob
from PyFlac.Carbon import CarbonSocketLayer
from PyFlac import TimeCode
import sys


SMB_PATH_IN = '\\\\10.221.173.231\\Avid\\'
SMB_PATH = '\\\\10.221.173.151\\Avid\\out'
GUID     = '{0216E844-5CF4-4388-B28E-1B692AF0CC46}'

def AvidCut(jobfile='', log_name=''):

	carbon = CarbonSocketLayer.CarbonSocketLayer("10.221.173.31")
	lst = []

        jf = open(jobfile, "r")
        fl = jf.readline()
	mxfile,tcinit = fl.split(",")

	tc = TimeCode.TimeCode(tcinit)
	

        if mxfile != '':
		print "MXF File: " + mxfile
		
                clips = jf.readline()
                while clips != '':

                        (cn, cin, cout) = clips.split(',')
			print "Clip " + cn + " In: " + cin + " Out: " + cout
			tin = TimeCode.TimeCode(cin)
			tout = TimeCode.TimeCode(cout)

			framesin  = tin.GetFramesFromZero() - tc.GetFramesFromZero()
			framesout = tout.GetFramesFromZero() - tc.GetFramesFromZero()
		
			print "Frames in: " + str(framesin)
			print "Frames out: " + str(framesout)

			lst.append( { 'in' : str(CarbonUtils.FramesToCarbonFramesUnit(framesin)), 'out': str(CarbonUtils.FramesToCarbonFramesUnit(framesout)) } )
			clips = jf.readline()			

		jobXml = CarbonUtils.CreateCarbonXMLJob(SMB_PATH_IN, mxfile, lst, [{'d_guid': GUID, 'd_basename': log_name, 'd_path': SMB_PATH}])	
		print jobXml

		job = CarbonJob.CarbonJob(carbon, jobXml)
		job.Start()

AvidCut("/data/Avid/Logueos_txt/" + sys.argv[1], sys.argv[1])
